package com.anolesoft.yr.mbus.base.mapper;

import com.anolesoft.yr.mbus.base.entity.BaseContactDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.LocalDateTimeTypeHandler;

import java.util.List;

/**
 * @author zhj
 * @date 2020/8/9 13:37
 */
@Mapper
public interface BaseContactMapper {
    @Insert("insert into base_contact (" +
            "objuid," +
            "fac_code," +
            "type," +
            "target_uid," +
            "contact_person," +
            "crm_address," +
            "contact_number," +
            "postcode," +
            "c_user_uid," +
            "c_time," +
            "m_user_uid," +
            "m_time," +
            "del_flag," +
            "remark" +
            ") values (" +
            "#{objuid,jdbcType=VARCHAR}," +
            "#{factoryCode,jdbcType=VARCHAR}," +
            "#{type,jdbcType=VARCHAR}," +
            "#{targetUid,jdbcType=VARCHAR}," +
            "#{contactPerson,jdbcType=VARCHAR}," +
            "#{crmAddress,jdbcType=VARCHAR}," +
            "#{contactNumber,jdbcType=VARCHAR}," +
            "#{postcode,jdbcType=VARCHAR}," +
            "#{createUserUid,jdbcType=VARCHAR}," +
            "now()," +
            "#{modifyUserUid,jdbcType=VARCHAR}," +
            "now()," +
            "0," +
            "#{remark,jdbcType=VARCHAR}" +
            ")")
    int insert(BaseContactDO baseContactDO);

    @Update("update base_contact set " +
            "fac_code = #{factoryCode,jdbcType=VARCHAR}," +
            "type = #{type,jdbcType=VARCHAR}," +
            "target_uid = #{targetUid,jdbcType=VARCHAR}," +
            "contact_person = #{contactPerson,jdbcType=VARCHAR}," +
            "crm_address = #{crmAddress,jdbcType=VARCHAR}," +
            "contact_number = #{contactNumber,jdbcType=VARCHAR}," +
            "postcode = #{postcode,jdbcType=VARCHAR}," +
            "m_user_uid = #{modifyUserUid,jdbcType=VARCHAR}," +
            "m_time = now()," +
            "remark = #{remark,jdbcType=VARCHAR}" +
            " where " +
            "objuid = #{objuid,jdbcType=VARCHAR}")
    int update(BaseContactDO baseContactDO);

    @Update("update base_contact set del_flag = 1 where objuid = #{id,jdbcType=VARCHAR}")
    int delete(@Param("id") String id);

    @Results(id = "BaseContactResultMap", value = {
            @Result(id = true, column = "objuid", property = "objuid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "fac_code", property = "factoryCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "type", property = "type", jdbcType = JdbcType.VARCHAR),
            @Result(column = "target_uid", property = "targetUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "contact_person", property = "contactPerson", jdbcType = JdbcType.VARCHAR),
            @Result(column = "crm_address", property = "crmAddress", jdbcType = JdbcType.VARCHAR),
            @Result(column = "contact_number", property = "contactNumber", jdbcType = JdbcType.VARCHAR),
            @Result(column = "postcode", property = "postcode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "c_user_uid", property = "createUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "c_time", property = "createTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "m_user_uid", property = "modifyUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "m_time", property = "modifyTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "del_flag", property = "deleteFlag", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR)
    })
    @Select("select objuid, fac_code, type, target_uid, contact_person, crm_address, contact_number, postcode," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from base_contact where del_flag = 0")
    List<BaseContactDO> selectAll();

    @ResultMap("BaseContactResultMap")
    @Select("select objuid, fac_code, type, target_uid, contact_person, crm_address, contact_number, postcode," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from base_contact where del_flag = 0")
    List<BaseContactDO> selectPageAll(Page<BaseContactDO> page);

    @ResultMap("BaseContactResultMap")
    @Select("select objuid, fac_code, type, target_uid, contact_person, crm_address, contact_number, postcode," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from base_contact where del_flag = 0 and objuid = #{id,jdbcType=VARCHAR}")
    BaseContactDO selectOneById(@Param("id") String id);

    @ResultMap("BaseContactResultMap")
    @Select("select objuid, fac_code, type, target_uid, contact_person, crm_address, contact_number, postcode," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from base_contact where del_flag = 0 and target_uid = #{targetUid,jdbcType=VARCHAR}")
    BaseContactDO selectListByTargetUid(@Param("targetUid") String targetUid);

    @ResultMap("BaseContactResultMap")
    @Select("<script>" +
            "select objuid, fac_code, type, target_uid, contact_person, crm_address, contact_number, postcode," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from base_contact where del_flag = 0 " +
            "<if test =\"o.factoryCode !=null and o.factoryCode != '' \">" +
            "   and fac_code = #{o.factoryCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.type !=null and o.type != '' \">" +
            "   and type = #{o.type, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.targetUid !=null and o.targetUid != '' \">" +
            "   and target_uid = #{o.targetUid, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.contactPerson !=null and o.contactPerson != '' \">" +
            "   and contact_person like #{o.contactPerson, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.crmAddress !=null and o.crmAddress != '' \">" +
            "   and crm_address like #{o.crmAddress, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.contactNumber !=null and o.contactNumber != '' \">" +
            "   and contact_number like #{o.contactNumber, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.postcode !=null and o.postcode != '' \">" +
            "   and postcode like #{o.postcode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.objuid !=null and o.objuid != '' \">" +
            "   and objuid = #{o.objuid, jdbcType=VARCHAR} " +
            "</if>" +
            "</script>")
    List<BaseContactDO> selectList(@Param("o") BaseContactDO baseContactDO);

    @ResultMap("BaseContactResultMap")
    @Select("<script>" +
            "select objuid, fac_code, type, target_uid, contact_person, crm_address, contact_number, postcode," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from base_contact where del_flag = 0 " +
            "<if test =\"o.factoryCode !=null and o.factoryCode != '' \">" +
            "   and fac_code = #{o.factoryCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.type !=null and o.type != '' \">" +
            "   and type = #{o.type, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.targetUid !=null and o.targetUid != '' \">" +
            "   and target_uid = #{o.targetUid, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.contactPerson !=null and o.contactPerson != '' \">" +
            "   and contact_person like #{o.contactPerson, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.crmAddress !=null and o.crmAddress != '' \">" +
            "   and crm_address like #{o.crmAddress, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.contactNumber !=null and o.contactNumber != '' \">" +
            "   and contact_number like #{o.contactNumber, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.postcode !=null and o.postcode != '' \">" +
            "   and postcode like #{o.postcode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.objuid !=null and o.objuid != '' \">" +
            "   and objuid = #{o.objuid, jdbcType=VARCHAR} " +
            "</if>" +
            "</script>")
    List<BaseContactDO> selectPageList(@Param("o") BaseContactDO baseContactDO, Page<BaseContactDO> page);
}
